package springboot.service.impl;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import springboot.service.IMailService;

import javax.annotation.Resource;

import java.io.File;

/**
 * @author janti
 * @date 2018/5/3 22:07
 */
@Component
public class MailServiceImpl implements IMailService {

  //  @Autowired
 //   private JavaMailSender mailSender;


    @Resource
    private TemplateEngine templateEngine;

    @Value("${spring.mail.username}")
    private String mailFrom;

    /**
     * 发送简单邮件
     *
     * @param to
     * @param subject
     * @param content
     */
    @Override
    public void sendSimpleEmail(String to,String subject,String content) {
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom(mailFrom);
//        message.setTo(to);
//        message.setSubject(subject);
//        message.setText(content);
//        mailSender.send(message);
    }

    /**
     * 发送html邮件
     *
     * @param to
     * @param subject
     * @param content
     */
    @Override
    public void sendHtmlMail(String to, String subject, String content) {
//        MimeMessage mimeMessage = mailSender.createMimeMessage();
//        try {
//            //true表示需要创建一个multipart message
//            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//            helper.setFrom(mailFrom);
//            helper.setTo(to);
//            helper.setSubject(subject);
//            helper.setText(content,true);
//            mailSender.send(mimeMessage);
//        } catch (MessagingException e) {
//            e.printStackTrace();
//      }
    }
    /**
     * 发送带附件的邮件
     *
     * @param to
     * @param subject
     * @param content
     * @param filepath
     */
    @Override
    public void sendFileMail(String to, String subject, String content, String filepath) {
//        MimeMessage mimeMessage = mailSender.createMimeMessage();
//        try {
//            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);
//            helper.setFrom(mailFrom);
//            helper.setTo(to);
//            helper.setSubject(subject);
//            helper.setText(content,true);
//
//            FileSystemResource file = new FileSystemResource(new File(filepath));
//            String fileName = filepath.substring(filepath.lastIndexOf(File.separator));
//            helper.addAttachment(fileName,file);
//
//            mailSender.send(mimeMessage);
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }
    }


    /**
     * 使用模板来发送邮件
     *
     * @param to
     * @param subject
     */
    @Override
    public void sendTemplateMail(String to, String subject) {
//        Context context = new Context();
//        context.setVariable("username","guigai");
//        String mailHtml =templateEngine.process("mail",context);
//        sendHtmlMail(to,subject,mailHtml);
    }


}
